package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Date;
import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.CheckBox;

@SuppressWarnings("deprecation")
public class TemplatesEventContent extends Content implements Serializable {
	private static final long serialVersionUID = -1189011235559691201L;
	private DatabaseHandlerServiceAsync databaseHandlerService = (DatabaseHandlerServiceAsync) GWT.create(DatabaseHandlerService.class);
	private final String relogString = "If this problem persists try relogging into the system.";
	
	Vector<EventTemplate> eventTemplateVector = new Vector<EventTemplate>();
	
	// the following are used when editing events
	Vector<Group> groupVector = new Vector<Group>();
	Vector<Person> personVector = new Vector<Person>();
	Vector<Group> addedGroupVector = new Vector<Group>();
	Vector<Person> addedPersonVector = new Vector<Person>();
	
	EventTemplate cancelEvent;
	EventTemplate selectedEvent;
	EventTemplate eventCopy;
	Group selectedGroup;
	Person selectedPerson;
	Date newStartDate;
	Date newEndDate;
	String recurrence;
	
	// UI Widgets
	ListBox eventTemplateListbox;
	TextBox eventNameTextbox;
	TextArea eventDescriptionTextBox;
	ListBox groupsListBox;
	Button addEventButton;
	Button deleteEventButton;
	Button removeGroupButton;
	Button addGroupButton;
	Button editEventButton;
	Button saveEventTemplateButton;
	Button cancelButton;
	PopupPanel popup;
	AddGroupPopupPanel agpp;
	AddPersonPopupPanel appp;
	ListBox startTimeHourCombobox;
	ListBox startTimeMinuteCombobox;
	ListBox startTimeAMPMCombobox;
	ListBox endTimeHourCombobox;
	ListBox endTimeMinuteCombobox;
	ListBox endTimeAMPMCombobox;
	CheckBox recurringEventComboBox;
	Hyperlink hprlnkEdit;
	Label lblAdditionalPeople;
	ListBox personListBox;
	Button addPersonButton;
	Button removePersonButton;
	Label lblStartTime;
	Label lblEndTime;
	Label repeatsLabel;
	Label repeatPatternLabel;
	
	public TemplatesEventContent() {
		
		ContentPanel.getInstance().showLoading(1, 80, 350);
		databaseHandlerService.getAllEventTemplates(new AsyncCallback<Vector<EventTemplate>>() {
			
			@Override
			public void onSuccess(Vector<EventTemplate> result) {
				if (result != null && !result.isEmpty()) {
					for (EventTemplate event : result) {
						if (event == null)
							continue;
						
						eventTemplateVector.add(event);
						eventTemplateListbox.addItem(event.getName());
					}
					ContentPanel.getInstance().hideLoadingPopup(1);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				ContentPanel.getInstance().hideLoadingPopup(1);
				Window.alert("Error: Failed to retrieve list of Event Templates. Please try reloading the page. " + relogString);
				return;
			}
		});	
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("1121px", "899px");
		
		Label lblSelectEvent = new Label("Event Templates:");
		absolutePanel.add(lblSelectEvent, 10, 10);
		
		Label lblEventName = new Label("Event Name:");
		absolutePanel.add(lblEventName, 319, 10);
		lblEventName.setSize("107px", "18px");
		
		eventNameTextbox = new TextBox();
		absolutePanel.add(eventNameTextbox, 319, 34);
		eventNameTextbox.setSize("364px", "13px");
		
		eventDescriptionTextBox = new TextArea();
		absolutePanel.add(eventDescriptionTextBox, 319, 109);
		eventDescriptionTextBox.setSize("368px", "156px");
		
		Label lblEventDescription = new Label("Event Description:");
		absolutePanel.add(lblEventDescription, 319, 85);
		lblEventDescription.setSize("122px", "18px");
		
		groupsListBox = new ListBox();
		groupsListBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (groupsListBox.getSelectedIndex() != -1) {
					selectedGroup = addedGroupVector.get(groupsListBox.getSelectedIndex());
				}
			}
		});
		absolutePanel.add(groupsListBox, 319, 339);
		groupsListBox.setSize("376px", "160px");
		groupsListBox.setVisibleItemCount(9);
		
		Label lblAssociatedGroups = new Label("Associated Groups:");
		absolutePanel.add(lblAssociatedGroups, 319, 315);
		lblAssociatedGroups.setSize("122px", "18px");
		
		addEventButton = new Button("Add");
		addEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clearForm();
				
				eventTemplateListbox.setSelectedIndex(-1);
				selectedEvent = null;
				newStartDate = null;
				newEndDate = null;
				
				groupVector = new Vector<Group>();
				addedGroupVector = new Vector<Group>();
				databaseHandlerService.getAllGroups(new AsyncCallback<Vector<Group>>() {
					
					@Override
					public void onSuccess(Vector<Group> result) {
						if (result != null && !result.isEmpty()) {
							for (Group group : result) {
								if (group == null)
									continue;
																
								groupVector.add(group);
							}
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error: Failed to retrieve Groups. " + relogString);
					}
				});
				
				personVector = new Vector<Person>();
				addedPersonVector = new Vector<Person>();
				databaseHandlerService.getAllPeople(new AsyncCallback<Vector<Person>>() {
					
					@Override
					public void onSuccess(Vector<Person> result) {
						if (result != null && !result.isEmpty()) {
							for (Person person : result) {
								if (person == null)
									continue;
								
								if (person.getUsername().equals("dev"))
									continue;
								
								personVector.add(person);
							}
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error: Failed to retrieve Volunteers. " + relogString);
					}
				});
				
				eventNameTextbox.setText("New Event Template");
				
				enableForm();
				eventTemplateListbox.setEnabled(false);
			}
		});
		absolutePanel.add(addEventButton, 10, 604);
		
		deleteEventButton = new Button("Delete");
		deleteEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedEvent != null && eventTemplateListbox.getSelectedIndex() != -1) {
					popup = new PopupPanel();
					popup.setPopupPosition(400, 400);
					VerticalPanel vp = new VerticalPanel();
					vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
					vp.add(new Label("Are you sure you want to delete " + selectedEvent.getName() + "? You cannot undo this action."));
					
					Button yesButton = new Button("Yes");
					yesButton.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							popup.hide();
							popup.clear();
							databaseHandlerService.delete(selectedEvent, new AsyncCallback<Boolean>() {
								
								@Override
								public void onSuccess(Boolean result) {
									if (result) {
										eventTemplateVector.remove(eventTemplateListbox.getSelectedIndex());
										eventTemplateListbox.removeItem(eventTemplateListbox.getSelectedIndex());
										selectedEvent = null;
										clearForm();
										disableForm();
										Window.alert("Event Template successfully deleted.");
									}
									else {
										Window.alert("An error ocurred while trying to delete the Event Template. Please try again. " + relogString);
									}
								}
								
								@Override
								public void onFailure(Throwable caught) {
									Window.alert("An error ocurred while trying to delete the Event Template. Please try again. " + relogString);
								}
							});
						}
					});
					
					Button noButton = new Button("No");
					noButton.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							popup.hide();
							popup.clear();
						}
					});
					
					SimplePanel sp = new SimplePanel();
					sp.setSize("5px", "20px");
					
					HorizontalPanel hp = new HorizontalPanel();
					hp.add(yesButton);
					hp.add(new Label("         "));
					hp.add(noButton);
					
					vp.add(sp);
					vp.add(hp);
					popup.add(vp);
					popup.show();
				}
			}
		});
		absolutePanel.add(deleteEventButton, 54, 604);
		deleteEventButton.setSize("53px", "24px");
		
		removeGroupButton = new Button("Add");
		removeGroupButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedGroup != null && groupsListBox.getSelectedIndex() != -1) {
					
					boolean duplicate = false;
					for (Group group : groupVector) {
						if (group == null)
							continue;
						
						if (group.getID() == selectedGroup.getID()) {
							duplicate = true;
							break;
						}
					}
					
					if (!duplicate) {
						groupVector.add(selectedGroup);
					}
					addedGroupVector.remove(selectedGroup);
					groupsListBox.removeItem(groupsListBox.getSelectedIndex());
					groupVector.add(selectedGroup);
					selectedGroup = null;
				}
			}
		});
		removeGroupButton.setText("Remove");
		absolutePanel.add(removeGroupButton, 363, 505);
		removeGroupButton.setSize("63px", "24px");
		
		addGroupButton = new Button("Add");
		addGroupButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {			
				//TODO: show loading
				agpp = new AddGroupPopupPanel(groupVector);
				agpp.setPopupPosition(eventDescriptionTextBox.getAbsoluteLeft(), eventDescriptionTextBox.getAbsoluteTop());
				agpp.setGlassEnabled(true);
				agpp.addGroupsButton.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						if (agpp.addedGroupVector != null && !agpp.addedGroupVector.isEmpty()) {
							for (Group group: agpp.addedGroupVector) {
								if (group == null)
									continue;
								
								groupsListBox.addItem(group.getName());
								addedGroupVector.add(group);
							}	
						}
						
						groupVector = new Vector<Group>(agpp.groupVector);
						
						agpp.hide();
						agpp.clear();
					}
				});
				agpp.cancelButton.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						agpp.hide();
						agpp.clear();
					}
				});				
				
				agpp.show();
			}
		});
		absolutePanel.add(addGroupButton, 319, 505);
		addGroupButton.setSize("38px", "24px");
		
		editEventButton = new Button("Edit");
		editEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (eventTemplateListbox.getSelectedIndex() != -1) {
					enableForm();
					eventTemplateListbox.setEnabled(false);
				}
			}
		});
		absolutePanel.add(editEventButton, 113, 604);
		editEventButton.setSize("38px", "24px");
		
		eventTemplateListbox = new ListBox();
		eventTemplateListbox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				populateForm();
				if (selectedEvent != null) {
					cancelEvent = new EventTemplate(selectedEvent);
				}
			}
		});
		eventTemplateListbox.setVisibleItemCount(30);
		absolutePanel.add(eventTemplateListbox, 10, 34);
		eventTemplateListbox.setSize("259px", "564px");
		
		saveEventTemplateButton = new Button("Save Event");
		saveEventTemplateButton.setText("Save");
		saveEventTemplateButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
								
				if (eventNameTextbox.getText().replace(" ","").equals("")) {
					Window.alert("The Template has no name!");
					return;
				}
				if (eventDescriptionTextBox.getText().replace(" ","").equals("")) {
					Window.alert("The Template has no description!");
					return;
				}
								
				if (selectedEvent != null) {
					eventCopy = new EventTemplate(selectedEvent);
				}
				else {
					selectedEvent = new EventTemplate();
				}
				
				newStartDate = new Date();
				newEndDate = new Date();
								
				selectedEvent.setName(eventNameTextbox.getText());
				selectedEvent.setDescription(eventDescriptionTextBox.getText());
				
				if (startTimeAMPMCombobox.getSelectedIndex() == 0) {
					if (startTimeHourCombobox.getSelectedIndex() == 11) {
						newStartDate.setHours(0);
					}
					else {
						newStartDate.setHours(startTimeHourCombobox.getSelectedIndex() + 1);
					}
				}
				else {
					if (startTimeHourCombobox.getSelectedIndex() == 12) {
						newStartDate.setHours(12);
					}
					else {
						newStartDate.setHours(startTimeHourCombobox.getSelectedIndex() + 13);
					}
				}
												
				if (endTimeAMPMCombobox.getSelectedIndex() == 0) {
					if (endTimeHourCombobox.getSelectedIndex() == 11) {
						newEndDate.setHours(0);
					}
					else {
						newEndDate.setHours(endTimeHourCombobox.getSelectedIndex() + 1);
					}
				}
				else {
					if (endTimeHourCombobox.getSelectedIndex() == 13) {
						newEndDate.setHours(12);
					}
					else {
						newEndDate.setHours(endTimeHourCombobox.getSelectedIndex() + 13);
					}
				}
				
				newStartDate.setMinutes(startTimeMinuteCombobox.getSelectedIndex());
				newEndDate.setMinutes(endTimeMinuteCombobox.getSelectedIndex());
				
				selectedEvent.setStartTime(newStartDate);
				selectedEvent.setEndTime(newEndDate);
								
				selectedEvent.setGroups(new Vector<Group>());
				
				if (groupsListBox.getItemCount() > 0) {
					for (Group group : addedGroupVector) {
						if (group == null)
							continue;
						
						selectedEvent.getGroups().add(group);
					}
				}
				
				selectedEvent.setPeople(new Vector<Person>());
					
				if (personListBox.getItemCount() > 0) {
					for (Person person : addedPersonVector) {
						if (person == null)
							continue;

						selectedEvent.getPeople().add(person);
					}
				}
				
				//TODO: recurrence
				databaseHandlerService.write(selectedEvent, new AsyncCallback<Integer>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error: Failed to save Event. " + relogString + caught.toString());
						if (eventCopy != null) {
							selectedEvent = new EventTemplate(eventCopy);
						}
					}

					@Override
					public void onSuccess(Integer result) {
						if (result > -1) {
							if (selectedEvent.getId() < 0)
								selectedEvent.setId(result);
														
							if (eventCopy == null) {
								eventTemplateListbox.addItem(selectedEvent.getName());
								eventTemplateVector.add(selectedEvent);
								
								if (eventTemplateListbox.getItemCount() > 0) 
									eventTemplateListbox.setSelectedIndex(groupVector.size() - 1);
							}
							
							disableForm();
							eventTemplateListbox.setEnabled(true);
							Window.alert("The Event Template was saved succesfully.");
						}
					}
				});
			}
		});
		absolutePanel.add(saveEventTemplateButton, 10, 604);
		saveEventTemplateButton.setSize("97px", "24px");
		
		cancelButton = new Button("Cancel");
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (cancelEvent != null) {
					selectedEvent = new EventTemplate(cancelEvent);
				}
					
				if (selectedEvent != null && eventTemplateListbox.getSelectedIndex() != -1) {
					populateForm();
					disableForm();
				}
				else {
					clearForm();
					disableForm();
				}
				eventTemplateListbox.setEnabled(true);
			}
		});
		absolutePanel.add(cancelButton, 113, 604);
		cancelButton.setSize("97px", "24px");
		
		lblAdditionalPeople = new Label("Additional Volunteers:");
		absolutePanel.add(lblAdditionalPeople, 735, 315);
		lblAdditionalPeople.setSize("134px", "18px");
		
		personListBox = new ListBox();
		personListBox.setVisibleItemCount(9);
		personListBox.setEnabled(false);
		absolutePanel.add(personListBox, 735, 339);
		personListBox.setSize("376px", "160px");
		personListBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (personListBox.getSelectedIndex() != -1) {
					selectedPerson = addedPersonVector.get(personListBox.getSelectedIndex());
				}
			}
		});
		
		addPersonButton = new Button("Add");
		addPersonButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//TODO: show loading
				appp = new AddPersonPopupPanel(personVector);
				appp.setPopupPosition(eventDescriptionTextBox.getAbsoluteLeft(), eventDescriptionTextBox.getAbsoluteTop());
				appp.setGlassEnabled(true);
				
				appp.personVector = new Vector<Person>();
				appp.addedPersonVector = new Vector<Person>();
				appp.addPersonButton.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						if (appp.addedPersonVector != null && !appp.addedPersonVector.isEmpty()) {
							for (Person person: appp.addedPersonVector) {
								if (person == null)
									continue;
								
								personListBox.addItem(person.getFirstName() + " " + person.getLastName());
								addedPersonVector.add(person);
							}	
						}
						
						personVector = new Vector<Person>(appp.personVector);
						
						appp.hide();
						appp.clear();
					}
				});
				appp.cancelButton.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						appp.hide();
						appp.clear();
					}
				});				
				
				appp.show();
			}
		});
		addPersonButton.setEnabled(false);
		absolutePanel.add(addPersonButton, 735, 505);
		addPersonButton.setSize("38px", "24px");
		
		removePersonButton = new Button("Remove");
		removePersonButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedPerson != null && personListBox.getSelectedIndex() != -1) {					
						personVector.add(selectedPerson);
						addedPersonVector.remove(selectedPerson);
						personListBox.removeItem(personListBox.getSelectedIndex());
						selectedPerson = null;				
				}
			}
		});
		removePersonButton.setEnabled(false);
		absolutePanel.add(removePersonButton, 779, 505);
		removePersonButton.setSize("63px", "24px");
		
		lblStartTime = new Label("Start Time:");
		absolutePanel.add(lblStartTime, 735, 10);
		lblStartTime.setSize("70px", "18px");
		
		lblEndTime = new Label("End Time:");
		absolutePanel.add(lblEndTime, 735, 76);
		lblEndTime.setSize("70px", "18px");
		
		startTimeHourCombobox = new ListBox();
		startTimeHourCombobox.addItem("1");
		startTimeHourCombobox.addItem("2");
		startTimeHourCombobox.addItem("3");
		startTimeHourCombobox.addItem("4");
		startTimeHourCombobox.addItem("5");
		startTimeHourCombobox.addItem("6");
		startTimeHourCombobox.addItem("7");
		startTimeHourCombobox.addItem("8");
		startTimeHourCombobox.addItem("9");
		startTimeHourCombobox.addItem("10");
		startTimeHourCombobox.addItem("11");
		startTimeHourCombobox.addItem("12");
		absolutePanel.add(startTimeHourCombobox, 735, 34);
		startTimeHourCombobox.setSize("50px", "18px");
		
		startTimeMinuteCombobox = new ListBox();
		startTimeMinuteCombobox.addItem("00");
		startTimeMinuteCombobox.addItem("01");
		startTimeMinuteCombobox.addItem("02");
		startTimeMinuteCombobox.addItem("03");
		startTimeMinuteCombobox.addItem("04");
		startTimeMinuteCombobox.addItem("05");
		startTimeMinuteCombobox.addItem("06");
		startTimeMinuteCombobox.addItem("07");
		startTimeMinuteCombobox.addItem("08");
		startTimeMinuteCombobox.addItem("09");
		startTimeMinuteCombobox.addItem("10");
		startTimeMinuteCombobox.addItem("11");
		startTimeMinuteCombobox.addItem("12");
		startTimeMinuteCombobox.addItem("13");
		startTimeMinuteCombobox.addItem("14");
		startTimeMinuteCombobox.addItem("15");
		startTimeMinuteCombobox.addItem("16");
		startTimeMinuteCombobox.addItem("17");
		startTimeMinuteCombobox.addItem("18");
		startTimeMinuteCombobox.addItem("19");
		startTimeMinuteCombobox.addItem("20");
		startTimeMinuteCombobox.addItem("21");
		startTimeMinuteCombobox.addItem("22");
		startTimeMinuteCombobox.addItem("23");
		startTimeMinuteCombobox.addItem("24");
		startTimeMinuteCombobox.addItem("25");
		startTimeMinuteCombobox.addItem("26");
		startTimeMinuteCombobox.addItem("27");
		startTimeMinuteCombobox.addItem("28");
		startTimeMinuteCombobox.addItem("29");
		startTimeMinuteCombobox.addItem("30");
		startTimeMinuteCombobox.addItem("31");
		startTimeMinuteCombobox.addItem("32");
		startTimeMinuteCombobox.addItem("33");
		startTimeMinuteCombobox.addItem("34");
		startTimeMinuteCombobox.addItem("35");
		startTimeMinuteCombobox.addItem("36");
		startTimeMinuteCombobox.addItem("37");
		startTimeMinuteCombobox.addItem("38");
		startTimeMinuteCombobox.addItem("39");
		startTimeMinuteCombobox.addItem("40");
		startTimeMinuteCombobox.addItem("41");
		startTimeMinuteCombobox.addItem("42");
		startTimeMinuteCombobox.addItem("43");
		startTimeMinuteCombobox.addItem("44");
		startTimeMinuteCombobox.addItem("45");
		startTimeMinuteCombobox.addItem("46");
		startTimeMinuteCombobox.addItem("47");
		startTimeMinuteCombobox.addItem("48");
		startTimeMinuteCombobox.addItem("49");
		startTimeMinuteCombobox.addItem("50");
		startTimeMinuteCombobox.addItem("51");
		startTimeMinuteCombobox.addItem("52");
		startTimeMinuteCombobox.addItem("53");
		startTimeMinuteCombobox.addItem("54");
		startTimeMinuteCombobox.addItem("55");
		startTimeMinuteCombobox.addItem("56");
		startTimeMinuteCombobox.addItem("57");
		startTimeMinuteCombobox.addItem("58");
		startTimeMinuteCombobox.addItem("59");
		absolutePanel.add(startTimeMinuteCombobox, 790, 34);
		startTimeMinuteCombobox.setSize("50px", "18px");
		
		startTimeAMPMCombobox = new ListBox();
		startTimeAMPMCombobox.addItem("AM");
		startTimeAMPMCombobox.addItem("PM");
		absolutePanel.add(startTimeAMPMCombobox, 858, 34);
		startTimeAMPMCombobox.setSize("53px", "18px");
		
		endTimeHourCombobox = new ListBox();
		endTimeHourCombobox.addItem("1");
		endTimeHourCombobox.addItem("2");
		endTimeHourCombobox.addItem("3");
		endTimeHourCombobox.addItem("4");
		endTimeHourCombobox.addItem("5");
		endTimeHourCombobox.addItem("6");
		endTimeHourCombobox.addItem("7");
		endTimeHourCombobox.addItem("8");
		endTimeHourCombobox.addItem("9");
		endTimeHourCombobox.addItem("10");
		endTimeHourCombobox.addItem("11");
		endTimeHourCombobox.addItem("12");
		absolutePanel.add(endTimeHourCombobox, 735, 100);
		endTimeHourCombobox.setSize("51px", "18px");
		
		endTimeMinuteCombobox = new ListBox();
		endTimeMinuteCombobox.addItem("00");
		endTimeMinuteCombobox.addItem("01");
		endTimeMinuteCombobox.addItem("02");
		endTimeMinuteCombobox.addItem("03");
		endTimeMinuteCombobox.addItem("04");
		endTimeMinuteCombobox.addItem("05");
		endTimeMinuteCombobox.addItem("06");
		endTimeMinuteCombobox.addItem("07");
		endTimeMinuteCombobox.addItem("08");
		endTimeMinuteCombobox.addItem("09");
		endTimeMinuteCombobox.addItem("10");
		endTimeMinuteCombobox.addItem("11");
		endTimeMinuteCombobox.addItem("12");
		endTimeMinuteCombobox.addItem("13");
		endTimeMinuteCombobox.addItem("14");
		endTimeMinuteCombobox.addItem("15");
		endTimeMinuteCombobox.addItem("16");
		endTimeMinuteCombobox.addItem("17");
		endTimeMinuteCombobox.addItem("18");
		endTimeMinuteCombobox.addItem("19");
		endTimeMinuteCombobox.addItem("20");
		endTimeMinuteCombobox.addItem("21");
		endTimeMinuteCombobox.addItem("22");
		endTimeMinuteCombobox.addItem("23");
		endTimeMinuteCombobox.addItem("24");
		endTimeMinuteCombobox.addItem("25");
		endTimeMinuteCombobox.addItem("26");
		endTimeMinuteCombobox.addItem("27");
		endTimeMinuteCombobox.addItem("28");
		endTimeMinuteCombobox.addItem("29");
		endTimeMinuteCombobox.addItem("30");
		endTimeMinuteCombobox.addItem("31");
		endTimeMinuteCombobox.addItem("32");
		endTimeMinuteCombobox.addItem("33");
		endTimeMinuteCombobox.addItem("34");
		endTimeMinuteCombobox.addItem("35");
		endTimeMinuteCombobox.addItem("36");
		endTimeMinuteCombobox.addItem("37");
		endTimeMinuteCombobox.addItem("38");
		endTimeMinuteCombobox.addItem("39");
		endTimeMinuteCombobox.addItem("40");
		endTimeMinuteCombobox.addItem("41");
		endTimeMinuteCombobox.addItem("42");
		endTimeMinuteCombobox.addItem("43");
		endTimeMinuteCombobox.addItem("44");
		endTimeMinuteCombobox.addItem("45");
		endTimeMinuteCombobox.addItem("46");
		endTimeMinuteCombobox.addItem("47");
		endTimeMinuteCombobox.addItem("48");
		endTimeMinuteCombobox.addItem("49");
		endTimeMinuteCombobox.addItem("50");
		endTimeMinuteCombobox.addItem("51");
		endTimeMinuteCombobox.addItem("52");
		endTimeMinuteCombobox.addItem("53");
		endTimeMinuteCombobox.addItem("54");
		endTimeMinuteCombobox.addItem("55");
		endTimeMinuteCombobox.addItem("56");
		endTimeMinuteCombobox.addItem("57");
		endTimeMinuteCombobox.addItem("58");
		endTimeMinuteCombobox.addItem("59");
		absolutePanel.add(endTimeMinuteCombobox, 790, 100);
		endTimeMinuteCombobox.setSize("50px", "18px");
		
		endTimeAMPMCombobox = new ListBox();
		endTimeAMPMCombobox.addItem("AM");
		endTimeAMPMCombobox.addItem("PM");
		absolutePanel.add(endTimeAMPMCombobox, 858, 100);
		endTimeAMPMCombobox.setSize("53px", "18px");
		
		recurringEventComboBox = new CheckBox("This is a recurring event");
		recurringEventComboBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				if (recurringEventComboBox.getValue() && recurrence != null) {
					repeatsLabel.setVisible(true);
					repeatPatternLabel.setText(RecurrenceDialogBox.run(recurrence));
				}
				else {
					repeatsLabel.setVisible(false);
					repeatPatternLabel.setText("");
				}
			}
		});
		absolutePanel.add(recurringEventComboBox, 735, 141);
		
		startTimeHourCombobox.setSelectedIndex(11);
		startTimeAMPMCombobox.setSelectedIndex(0);
		endTimeHourCombobox.setSelectedIndex(11);
		endTimeAMPMCombobox.setSelectedIndex(0);
		
		repeatsLabel = new Label("Repeats:");
		absolutePanel.add(repeatsLabel, 735, 166);
		
		repeatPatternLabel = new Label("Doesn't repeat");
		absolutePanel.add(repeatPatternLabel, 790, 166);
		
		hprlnkEdit = new Hyperlink("Edit", false, "newHistoryToken");
		hprlnkEdit.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				//TODO: recurrence				
			}
		});
		absolutePanel.add(hprlnkEdit, 899, 141);
		hprlnkEdit.setVisible(false);
		
		disableForm();			
	}
	
	// populate form *************************************************************************************
	private void populateForm() {
		addedGroupVector = new Vector<Group>();
		addedPersonVector = new Vector<Person>();
		
		if (eventTemplateListbox.getSelectedIndex() != -1) {
			clearForm();
			selectedEvent = eventTemplateVector.get(eventTemplateListbox.getSelectedIndex());
			
			//TODO: show loading
			groupVector = new Vector<Group>();
			addedGroupVector = new Vector<Group>();
			databaseHandlerService.getAllGroups(new AsyncCallback<Vector<Group>>() {
				
				@Override
				public void onSuccess(Vector<Group> result) {
					if (result != null && !result.isEmpty()) {						
						for (Group group : result) {
							if (group == null)
								continue;
							
							if (selectedEvent.getGroups() != null) {
								boolean duplicate = false;
								for (Group gr : selectedEvent.getGroups()) {
									if (gr == null)
										continue;
									
									if (gr.getID() == group.getID()) {
										duplicate = true;
										break;
									}
								}
							
								if (!duplicate) {
									groupVector.add(group);
								}
								else {
									addedGroupVector.add(group);
									groupsListBox.addItem(group.getName());
								}
							}
							else {
								groupVector.add(group);
							}
						}
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					Window.alert("Error: Failed to retrieve Groups. " + relogString);
				}
			});
			
			//TODO: show loading
			personVector = new Vector<Person>();
			addedPersonVector = new Vector<Person>();
			databaseHandlerService.getAllPeople(new AsyncCallback<Vector<Person>>() {
				
				@Override
				public void onSuccess(Vector<Person> result) {
					if (result != null && !result.isEmpty()) {
						for (Person person : result) {
							if (person == null)
								continue;
							
							if (person.getUsername().equals("dev"))
								continue;
							
							if (selectedEvent.getPeople() != null) {
								boolean duplicate = false;
								for (Person pr : selectedEvent.getPeople()) {
									if (pr == null)
										continue;
									
									if (pr.getID() == person.getID()) {
										duplicate = true;
										break;
									}
								}
							
								if (!duplicate) {
									personVector.add(person);
								}
								else {
									addedPersonVector.add(person);
									personListBox.addItem(person.getFirstName() + " " + person.getLastName());
								}
							}
							else {
								personVector.add(person);
							}
						}
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					Window.alert("Error: Failed to retrieve Volunteers. " + relogString);
				}
			});
			
			if (selectedEvent != null) {
				eventNameTextbox.setText(selectedEvent.getName());
				eventDescriptionTextBox.setText(selectedEvent.getDescription());
				
				if (selectedEvent.getStartTime().getHours() == 0) {
					startTimeHourCombobox.setItemSelected(11, true);
					startTimeAMPMCombobox.setItemSelected(0, true);
				}
				else if (selectedEvent.getStartTime().getHours() < 12) {
					startTimeHourCombobox.setItemSelected(selectedEvent.getStartTime().getHours() - 1, true);
					startTimeAMPMCombobox.setItemSelected(0, true);
				}
				else if (selectedEvent.getStartTime().getHours() == 12) {
					startTimeHourCombobox.setItemSelected(11, true);
					startTimeAMPMCombobox.setItemSelected(1, true);
				}
				else if (selectedEvent.getStartTime().getHours() > 12) {
					startTimeHourCombobox.setItemSelected(selectedEvent.getStartTime().getHours() - 13, true);
					startTimeAMPMCombobox.setItemSelected(1, true);
				}
				
				if (selectedEvent.getEndTime().getHours() == 0) {
					endTimeHourCombobox.setItemSelected(11, true);
					endTimeAMPMCombobox.setItemSelected(0, true);
				}
				else if (selectedEvent.getEndTime().getHours() < 12) {
					endTimeHourCombobox.setItemSelected(selectedEvent.getEndTime().getHours() - 1, true);
					endTimeAMPMCombobox.setItemSelected(0, true);
				}
				else if (selectedEvent.getEndTime().getHours() == 12) {
					endTimeHourCombobox.setItemSelected(11, true);
					endTimeAMPMCombobox.setItemSelected(1, true);
				}
				else if (selectedEvent.getEndTime().getHours() > 12) {
					endTimeHourCombobox.setItemSelected(selectedEvent.getEndTime().getHours() - 13, true);
					endTimeAMPMCombobox.setItemSelected(1, true);
				}
				
				startTimeMinuteCombobox.setItemSelected(selectedEvent.getStartTime().getMinutes(), true);
				endTimeMinuteCombobox.setItemSelected(selectedEvent.getEndTime().getMinutes(), true);
				
				eventDescriptionTextBox.setText(selectedEvent.getDescription());
				
				//TODO: show recurrence
			}
			
			disableForm();
			eventTemplateListbox.setEnabled(true);
		}
	}
	
	private void disableForm() {		
		recurringEventComboBox.setVisible(false);
		
		eventNameTextbox.setEnabled(false);
		eventDescriptionTextBox.setEnabled(false);
		
		groupsListBox.setEnabled(false);
		addGroupButton.setEnabled(false);
		removeGroupButton.setEnabled(false);
		
		personListBox.setEnabled(false);
		addPersonButton.setEnabled(false);
		removePersonButton.setEnabled(false);
		
		startTimeAMPMCombobox.setEnabled(false);
		startTimeHourCombobox.setEnabled(false);
		startTimeMinuteCombobox.setEnabled(false);
		
		endTimeAMPMCombobox.setEnabled(false);
		endTimeHourCombobox.setEnabled(false);
		endTimeMinuteCombobox.setEnabled(false);
		
		recurringEventComboBox.setEnabled(false);
		repeatsLabel.setVisible(false);
		repeatPatternLabel.setVisible(false);
		hprlnkEdit.setVisible(false);
		
		addEventButton.setEnabled(true);
		editEventButton.setEnabled(true);
		deleteEventButton.setEnabled(true);
		
		saveEventTemplateButton.setVisible(false);
		cancelButton.setVisible(false);
	}
	
	private void enableForm() {
		eventNameTextbox.setEnabled(true);
		eventDescriptionTextBox.setEnabled(true);
		
		groupsListBox.setEnabled(true);
		addGroupButton.setEnabled(true);
		removeGroupButton.setEnabled(true);
		
		personListBox.setEnabled(true);
		addPersonButton.setEnabled(true);
		removePersonButton.setEnabled(true);
		
		startTimeAMPMCombobox.setEnabled(true);
		startTimeHourCombobox.setEnabled(true);
		startTimeMinuteCombobox.setEnabled(true);
		
		endTimeAMPMCombobox.setEnabled(true);
		endTimeHourCombobox.setEnabled(true);
		endTimeMinuteCombobox.setEnabled(true);
		
		recurringEventComboBox.setEnabled(true);
		
		addEventButton.setEnabled(false);
		editEventButton.setEnabled(false);
		deleteEventButton.setEnabled(false);
		
		saveEventTemplateButton.setVisible(true);
		cancelButton.setVisible(true);
	}
	
	private void clearForm() {
		eventNameTextbox.setText("");
		eventDescriptionTextBox.setText("");
		
		startTimeAMPMCombobox.setItemSelected(0, false);
		startTimeHourCombobox.setItemSelected(0, false);
		startTimeMinuteCombobox.setItemSelected(0, false);
		
		endTimeAMPMCombobox.setItemSelected(0, false);
		endTimeHourCombobox.setItemSelected(0, false);
		endTimeMinuteCombobox.setItemSelected(0, false);
		
		recurringEventComboBox.setValue(false);
		
		groupsListBox.clear();
		personListBox.clear();
	}
}
